<template>
	<view>
		<city-select @cityClick="cityClick" :formatName="formatName" :activeCity="activeCity" :hotCity="hotCity"
			:obtainCitys="obtainCitys" :isSearch="true" ref="citys"></city-select>
	</view>
</template>

<script>
	import citySelect from '../../components/city-select/city-select.vue'
	let QQMapWx = require('../../common/qqmap-wx-jssdk.min.js')
	let qqmap = new QQMapWx({key:'JBMBZ-CYVCZ-HNPXZ-TFNNX-YC5NV-5RF5L'})
	export default {
		data() {
			return {
				//需要构建索引参数的名称（注意：传递的对象里面必须要有这个名称的参数）
				formatName: 'name',
				//当前城市
				activeCity: null,
				//热门城市
				hotCity: [],
				//显示的城市数据
				obtainCitys: []
			}
		},
		components: {
			citySelect
		},
		async onLoad(e) {
			this.loadCities();

		},
		methods: {
			cityClick(item) {
				var pages = getCurrentPages();
				if (pages.length > 1) {
					var parent = pages[pages.length - 2];
					console.log(pages, parent)
					uni.setStorageSync("city",item)
				}
				uni.navigateBack()
			},
			async loadCities(){
				this.$http.get('/web/ai/city_list', {}, (res) => {
					this.hotCity = res.hot_citys
					this.obtainCitys = res.citys
					this.getLocation()
				})
			},
			async getLocation(){
				uni.getLocation({
					type:'wgs84',
					geocode:true,
					success:(res)=>{
						console.log(res,qqmap)
						qqmap.reverseGeocoder({
							location:{
								latitude:res.latitude,
								longitude:res.longitude
							},
							success:(mapres)=>{
							
								console.log(mapres,mapres.result.ad_info.city)
								var city= mapres.result.ad_info.city//adcode
								if(city){
									if(city.substr(city.length-1,1)==='市'){
										city = city.substr(0,city.length-1)
									}
									var locationCity = this.obtainCitys.filter(o=>o.name.indexOf(city)>=0)
									if(locationCity.length>0){
										this.activeCity = locationCity[0]
									}
								}
							},
							fail:(maperr)=>{
								uni.showModal({
									content:maperr.message
								})
								console.log(maperr)
								return ''
							}
						})
						return ''
					},
					fail:(err)=>{
						return ''
					}
				})
			}
		}
	}
</script>

<style></style>